package com.sbp.message.api.common;

//import java.util.regex.Pattern;

/**
 * Created by wangmin on 2018/4/18.
 */
public abstract class TopicUtils {
    //private static final Pattern consumersDescribePattern = Pattern.compile("^Consumer\\.[a-zA-Z]+\\.VirtualTopic\\.[a-zA-Z]+$");
    //public static boolean validateConsumersDescribePattern(String consumersDescribe) {
    //    return consumersDescribePattern.matcher(consumersDescribe).matches();
    //}

    public static String leaveOutInvalidChar(String appName) {
        StringBuilder sb = new StringBuilder();
        for (char ch : appName.toCharArray()) {
            if ((ch >='A' && ch <= 'Z') || (ch >='a' && ch <= 'z') || (ch >='0' && ch <= '9'))
                sb.append(ch);
        }
        return sb.toString();
    }
    public static String generateTopicMessageDubboGroupName(String appName, String topicSuffix) {
        return leaveOutInvalidChar(appName) + ".VirtualTopic." + leaveOutInvalidChar(topicSuffix);
    }
    public static String generateConsumersDescribe(String appName, String topicSuffix) {
        return "Consumer." + generateTopicMessageDubboGroupName(appName, topicSuffix);
    }

    public static String generateDestinationFromConsumersDescribe(String consumersDescribe) {
        int lastDotIdx = consumersDescribe.lastIndexOf('.');
        return "VirtualTopic" + consumersDescribe.substring(lastDotIdx);
    }
}
